package org.example.network.demo7;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 给学校的老师写的， 不具有泛用性， 仅对 https://web.stanford.edu/class/ee368/Handouts/Lectures/Examples/有效
 */
@Slf4j
public class Demo {
    public static void main(String[] args) {
        List<String> files = getAndChild("");
        log.info("【资源分析】分析完毕，资源数->{}",files.size());

        log.info("【资源拉取】正在拉取中...");
        for (String fileUrl : files) {
            HttpUtil.downloadFile(Setting.resourcePath+fileUrl,Setting.dir+fileUrl);
//            System.out.println(fileUrl);
        }
    }


    public static List<String> getAndChild(String prefix){
        log.info("【资源分析】开始 prefix->{}",prefix);
        List<String> result = new ArrayList<>();

        String page = HttpUtil.get(Setting.resourcePath+prefix);
        boolean isResourcePage = validPage(page);
        boolean isMidPage = validMidPage(page);
        if (!isResourcePage&&!isMidPage){
            return result;
        }
        if (isMidPage){
            String[] split = page.split(Setting.midScope);
            List<String> dirs = Arrays.stream(split[1].split("<a href=\""))
                    .filter(x -> !(x.startsWith("\n\n")||x.startsWith("\r")))
                    .map(x -> x.split("\">")[0]).collect(Collectors.toList());
            for (String dir : dirs) {
                result.addAll(getAndChild(prefix+dir+"/"));
            }
            return result;
        }

        List<String> list = List.of(page.split(Setting.scope)[1].trim().split("\n"));

        for (String item : list) {
            String relativeUrl =  item.split("<a href=\"")[1].split("\"")[0];
            if (relativeUrl.startsWith("/")) {
                continue;
            }
            if (relativeUrl.endsWith("/")){
                result.addAll(getAndChild(prefix+relativeUrl));
            }else {
                result.add(prefix+relativeUrl);
            }
        }

        return result;
    }

    private static boolean validMidPage(String page) {
        int length = page.split(Setting.midScope).length;
        return length>=3;
    }

    private static boolean validPage(String page) {
        int length = page.split(Setting.scope).length;
        return length>=3;
    }
}
